var areaid;

function builds(level, floorurl, floorareaid, unlevel, mapAppId) {
    if (unlevel == 'undefined'||unlevel == '') { unlevel = '0' }
    level = parseInt(level) + parseInt(unlevel);
    $('.building-body-con-model').show();
    $('.building-body-con-node').show();
    $(".building").animate({ width: "100%", height: "100%", minHeight: "7.66rem" }, 500)
    areaid = floorareaid;
    $("#demo").show();
    $("#demo").animate({ width:"2rem", height: "3rem" });
    var D = {

        base: 1,
        size: 80,
        space: 16,
        height: {
            min: 10,
            max: 80
        },
        // type: ['a', 'b', 'c', 'd', 'e'],
        type: ['b'],
        bldg: '<a><b><b><b><i></i></b></b></b></a>',

        Random: function(min, max) {
            var value = Math.random() * (max - min) + min;
            var randomValue = Math.round(value);
            return randomValue;
        },


        Build: function() {
            var url = floorurl + floorareaid + "&appid=" + mapAppId;
            $.ajax({
                url: url,
                type: "get",
                async: false,
                contentType: "application/json",
                success: function(result) {
                    var result = eval("(" + result + ")");
                    var data = result.ret.value;
                    var badfloor = new Array();
                    var goodfloor = new Array();

                    for (i = 0; i < data.length; i++) {
                        if (floorurl == '/rest/firepm/global_floor_status?areaid=') {
                            var flag = data[i].issue;
                        } else {
                            var flag = data[i].sum;
                        }
                        if (flag > 0) {
                            badfloor.push(data[i].floor)
                        }
                    }
                    for (i = 0; i < data.length; i++) {
                        if (floorurl == '/rest/firepm/global_floor_status?areaid=') {
                            var flag = data[i].issue;
                        } else {
                            var flag = data[i].sum;
                        }
                        if (flag == 0) {
                            goodfloor.push(data[i].floor)
                        }
                    }


                    //楼宇形状定制
                    var e = D.data[0];
                    if (level < 10) {
                        var fh = 35;
                    } else if (10 <= level &&level< 17) {
                        var fh = 25;
                    } else if (17 <= level &&level< 25) {
                        var fh = 20;
                    } else {
                        var fh = 15;
                    }

                    var w = 100;
                    var h = 220;
                    var l = 50;
                    var t = 40;
                    var z = level * fh;
                    if (z<301) {
                        $("#demo").css('bottom',"15%");
                    }
                    var i = 0;
                    var c = e.getElementsByTagName('*');

                    e.className = D.type[i];

                    e.style.width = w + 'px';
                    e.style.height = h + 'px';
                    e.style.left = 0 * D.size + l + 'px';
                    e.style.top = 0 + t + 'px';

                    c[0].style.height = z + 'px';
                    c[1].style.width = h + 'px';
                    c[2].style.width = w + 'px';
                    c[3].style.width = h + 'px';
                    setWidthAndHeight(c, h, w, level, unlevel, goodfloor, badfloor, floorareaid, fh);
                    //楼宇形状定制结束


                }
            });


        },

        Create: function() {
            var div = document.createElement('div');
            div.innerHTML = D.bldg;
            D.demo.appendChild(div);
            D.data = D.demo.getElementsByTagName('div');

        },

        Init: function() {
            var l = D.base * D.size;
            D.demo = document.getElementById('demo');
            D.demo.style.width = 0 + 'px';
            D.demo.style.height = 0 + 'px';
            D.demo.style.marginTop = 0 + 'px';
            D.demo.style.marginLeft = 0 + 'px';
            //   D.demo.addEventListener('click', D.Build, false);
        },

        Run: function() {
            D.Init();
            D.Create();
            D.Build();
        }

    };
    D.Run();
}




//自定义楼宇长宽高生成
function setWidthAndHeight(c, h, w, level, unlevel, goodfloor, badfloor, floorareaid, fh) {
    var strdivW = "<div class='level' style='color: #fff;position: absolute;width:" + w + "px'>";
    for (var j = 0; j < level; j++) {
        var floorn = level - j - unlevel - 1;
        if (floorn >= 0) {
            floorn = floorn + 1;
        }
        if ($.inArray(floorn, goodfloor) > -1) {
            strdivW = strdivW + "<h6 style='line-height: " + fh + "px;color: rgba(0,0,0,0);' class='b-good' onclick='getPreData(0,8," + floorn + "," + floorareaid + ")'>" + floorn + "</h6>";
        } else if ($.inArray(floorn, badfloor) > -1) {
            strdivW = strdivW + "<h6 style='line-height: " + fh + "px;color: rgba(0,0,0,0);' class='b-bad' onclick='getPreData(0,8," + floorn + "," + floorareaid + ")'>" + floorn + "</h6>";
        } else {
            if (floorareaid == 782 && window.location.href.indexOf('home.html') > -1) {
                strdivW = strdivW + "<h6 style='line-height: " + fh + "px;color: rgba(0,0,0,0);' class='b-good' onclick='getPreData(0,8," + floorn + "," + floorareaid + ")'>" + floorn + "</h6>";
            } else {
                strdivW = strdivW + "<h6 style='line-height: " + fh + "px;color: rgba(0,0,0,0);'>" + floorn + "</h6>";
            }

        }
    }
    $(c[0]).append(strdivW);

    $(c[2]).append(strdivW);
    strdivW = strdivW + "</div>";

    var strdivH = "<div class='level' style='color: #fff;position: absolute;width:" + h + "px'>";
    for (var j = 0; j < level; j++) {
        var floorn = level - j - unlevel - 1;
        if (floorn >= 0) {
            floorn = floorn + 1;
        }
        if ($.inArray(floorn, goodfloor) > -1) {
            strdivH = strdivH + "<h6 style='line-height: " + fh + "px;' class='b-good' onclick='getPreData(0,8," + floorn + "," + floorareaid + ")'>" + floorn + "</h6>";
        } else if ($.inArray(floorn, badfloor) > -1) {
            strdivH = strdivH + "<h6 style='line-height: " + fh + "px;' class='b-bad' onclick='getPreData(0,8," + floorn + "," + floorareaid + ")'>" + floorn + "</h6>";
        } else {
            if (floorareaid == 782 && window.location.href.indexOf('home.html') > -1) {
                strdivH = strdivH + "<h6 style='line-height: " + fh + "px;' class='b-good' onclick='getPreData(0,8," + floorn + "," + floorareaid + ")'>" + floorn + "</h6>";
            } else {
                strdivH = strdivH + "<h6 style='line-height: " + fh + "px;'>" + floorn + "</h6>";
            }

        }
    }
    $(c[1]).append(strdivH);
    $(c[3]).append(strdivH);
    strdivH = strdivH + "</div>";


    c[4].style.width = h + 'px';
    c[4].style.height = w + 'px';


}